//
//  NSDate+Extension.m
//  恒好租车
//
//  Created by 123 on 15/12/28.
//  Copyright © 2015年 gg. All rights reserved.
//

#import "NSDate+Extension.h"

@implementation NSDate (Extension)
+ (NSDate *)stringToDate:(NSString *)str
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"YYYY-MM-dd";
    return [formatter dateFromString:str];
}
- (NSString *)convertDateToMonthSting
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"YYYY-MM";
    return [formatter stringFromDate:self];
}
- (NSString *)convertDateToSting;
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //@"YYYY-MM-dd HH:mm:ss"
    formatter.dateFormat = @"YYYY-MM-dd";
    return [formatter stringFromDate:self];
}

- (NSString *)convertDateToMinusSting
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    //@"YYYY-MM-dd HH:mm:ss"
    formatter.dateFormat = @"YYYY-MM-dd HH:mm";
    return [formatter stringFromDate:self];
}

+ (NSString *)spToDateString:(NSString *)sp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    formatter.dateFormat = @"YYYY-MM-dd";
    
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[sp floatValue]/1000];
    return [formatter stringFromDate:timeDate];
}
+ (NSString *)spToDateMinusString:(NSString *)sp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm";
    
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[sp floatValue]/1000];
    return [formatter stringFromDate:timeDate];
}
+ (NSDate *)spToDate:(NSString *)sp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm";
    
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[sp floatValue]/1000];
    return timeDate;
}

- (NSString *)dateToSp
{
    NSString *timeStr = [self convertDateToSting];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    NSDate *newDate = [formatter dateFromString:timeStr];
    return [NSString stringWithFormat:@"%lld",(long long)[newDate timeIntervalSince1970]*1000];
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    NSInteger interval = [zone secondsFromGMTForDate:self];
//    NSDate *localeDate = [self dateByAddingTimeInterval:interval];
//    return [NSString stringWithFormat:@"%ld",(long)[localeDate timeIntervalSince1970]];
}
/**比较2个日期之间的差值*/
- (NSDateComponents *)compareToDate:(NSDate *)date
{
    // 日历对象（方便比较两个日期之间的差距）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // NSCalendarUnit枚举代表想获得哪些差值
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour;
    // 计算两个日期之间的差值
    NSDateComponents *cmps = [calendar components:unit fromDate:self toDate:date options:0];
    return cmps;
}
- (NSDate *)addYears:(NSInteger)years days:(NSInteger)days hours:(NSInteger)hours minutes:(NSInteger)minutes
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *addCmp = [[NSDateComponents alloc]init];
    addCmp.year = years;
    addCmp.day = days;
    addCmp.hour = hours;
    addCmp.minute = minutes;
    NSDate *addDate = [calendar dateByAddingComponents:addCmp toDate:self options:0];
    return addDate;
}

- (NSDate *)addYears:(NSInteger)years months:(NSInteger)month days:(NSInteger)days hours:(NSInteger)hours minutes:(NSInteger)minutes
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *addCmp = [[NSDateComponents alloc]init];
    addCmp.year = years;
    addCmp.month = month;
    addCmp.day = days;
    addCmp.hour = hours;
    addCmp.minute = minutes;
    NSDate *addDate = [calendar dateByAddingComponents:addCmp toDate:self options:0];
    return addDate;
}

- (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
{
    NSDate *date8 = [self getCustomDateWithHour:fromHour];
    NSDate *date23 = [self getCustomDateWithHour:toHour];
    
    NSDate *currentDate = [NSDate date];
    
    if ([currentDate compare:date8]==NSOrderedDescending && [currentDate compare:date23]==NSOrderedAscending)
    {
        NSLog(@"该时间在 %d:00-%d:00 之间！", fromHour, toHour);
        return YES;
    }
    return NO;
}
- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    
    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    return [resultCalendar dateFromComponents:resultComps];
}

@end
